package com.uniledger.exchange.mine.scheme;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.uniledger.exchange.BuildConfig;
import com.uniledger.exchange.R;
import com.uniledger.exchange.UniApp;
import com.uniledger.exchange.base.activity.BaseActivity;
import com.uniledger.exchange.mine.MineFragment;
import com.uniledger.exchange.wallet.MnemonicActivity;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.internal.operators.completable.CompletableFromAction;
import io.reactivex.schedulers.Schedulers;

public class SettingActivity extends BaseActivity {

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, SettingActivity.class);
        activity.startActivityForResult(intent, MineFragment.REQUEST_CODE_OUT);
    }

    @BindView(R.id.btn_sign_out)
    Button signOut;

    @BindView(R.id.txt_version)
    TextView txt_version;

    @BindView(R.id.label_version)
    TextView label_version;

    @BindView(R.id.txt_backup)
    TextView txt_backup;

    @BindView(R.id.txt_keystore)
    TextView txt_keystore;

    @BindView(R.id.txt_lang)
    TextView txt_lang;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
    }

    @BindString(R.string.settting_bar_title)
    String title;

    @Override
    public void initBar() {
        super.initBar();
        toolbar.setTitle(title);
    }

    @Override
    public void getParams() {
        //TODO 初始化版本号
        txt_version.setText(BuildConfig.VERSION_NAME);
    }

    @OnClick({R.id.txt_version,R.id.label_version})
    void update(){
        Beta.checkUpgrade();
    }

    /**
     * 当前用户退出
     */
    @OnClick({R.id.btn_sign_out})
    void logout() {
        new CompletableFromAction(new Action() {
            @Override
            public void run() throws Exception {
                //清空数据
                UniApp.getDb().keyDao().deleteKey();
                UniApp.getDb().adminDao().deleteAdmin();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action() {
                    @Override
                    public void run() throws Exception {
                        //跳转
                        Intent intent = new Intent();
                        setResult(RESULT_OK, intent);
                        finish();//销毁当前页面
                    }
                });
    }

    @OnClick(R.id.txt_backup)
    void backup(){
        MnemonicActivity.start(SettingActivity.this);
    }

    @OnClick(R.id.txt_keystore)
    void keystore(){
        startActivity(new Intent(this,KeyStoreActivity.class));
    }

    @OnClick(R.id.txt_lang)
    void setLang(){
        SetLanguageActivity.start(this);
    }
}
